/**
 * Created by Administrator on 2015/1/21.
 */
var express = require('express');
var service = require('./service');
var router = express.Router();
var logger = require('./logger');
var Message = require('./message').message();

router.get('/map', function (req, res) {
    var query = req.query;
    var pass = service.verify(query);
    if (pass) {
        var echostr = query['echostr'];
        res.send(echostr);
    } else {
        res.send("bad token~");
    }
});

router.post('/map', function (req, res) {
    var xmlParam = req.body;
    var query = req.query;
    var pass = service.verify(query);
    if (!pass) {
        logger.info(Message.ILLEGAL_REQUEST);
        res.send(Message.ILLEGAL_REQUEST);
        return;
    }
    logger.debug("req body", xmlParam);
    var result = service.process(xmlParam);
    logger.info("view result", result);
    res.send(result);
});

router.get('/', function (req, res) {
    res.render('test');
});
router.get('/test', function (req, res) {
    var xmlParam = req.param("xml");
    var result = service.process(xmlParam);
    logger.info("view result", result);
    res.send({result: result});
});

module.exports = router;